//
//  EmoticonPackage.swift
//  testEmojKeyBoard
//
//  Created by lujun on 2021/11/23.
//

import UIKit

class EmoticonPackage: NSObject {
    var emoticons: [Emoticon] = [Emoticon]()
    
    init(id: String){
        super.init()
        if id == "" {
            //添加最近表情
            addEmptyEmoticon(isRecently: true)
            return
        }
        // 2.
        let plishPath = Bundle.main.path(forResource: "\(id)/info.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
       
        let array =  NSArray(contentsOfFile: plishPath)! as! [[String: String]]
        var index: Int = 0
        for var dict in array {
          if let png = dict["png"] {
                dict["png"] = id + "/" + png
                
            }
            emoticons.append(Emoticon(dict: dict))
            index += 1
            if index == 20 {
                emoticons.append(Emoticon(isRemove: true))
                index = 0
            }
         }
        //添加空白表情
        addEmptyEmoticon(isRecently: false)
      }
    
    //添加最近表情函数
    private func addEmptyEmoticon(isRecently: Bool){
        
        let count = emoticons.count % 21
        if count == 0 && !isRecently {
            return
        }
        
        for _ in count..<20{
            emoticons.append(Emoticon(isEmpty: true))
        }
        emoticons.append(Emoticon(isRemove: true))
        
    }
}




